package com.nanhai.buddhist.college.module.study.service.studyevaluation;

import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.nanhai.buddhist.college.module.study.controller.admin.studyevaluation.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyevaluation.StudyEvaluationDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.study.convert.studyevaluation.StudyEvaluationConvert;
import com.nanhai.buddhist.college.module.study.dal.mysql.studyevaluation.StudyEvaluationMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.*;

/**
 * 学生评价 Service 实现类
 *
 * @author AKai
 */
@Service
@Validated
public class StudyEvaluationServiceImpl implements StudyEvaluationService {

    @Resource
    private StudyEvaluationMapper evaluationMapper;

    @Override
    public String createEvaluation(StudyEvaluationCreateReqVO createReqVO) {
        // 插入
        StudyEvaluationDO evaluation = StudyEvaluationConvert.INSTANCE.convert(createReqVO);
        evaluationMapper.insert(evaluation);
        // 返回
        return evaluation.getId();
    }

    @Override
    public void updateEvaluation(StudyEvaluationUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateEvaluationExists(updateReqVO.getId());
        // 更新
        StudyEvaluationDO updateObj = StudyEvaluationConvert.INSTANCE.convert(updateReqVO);
        evaluationMapper.updateById(updateObj);
    }

    @Override
    public void deleteEvaluation(String id) {
        // 校验存在
        this.validateEvaluationExists(id);
        // 删除
        evaluationMapper.deleteById(id);
    }

    private void validateEvaluationExists(String id) {
        if (evaluationMapper.selectById(id) == null) {
            throw exception(EVALUATION_NOT_EXISTS);
        }
    }

    @Override
    public StudyEvaluationDO getEvaluation(String id) {
        return evaluationMapper.selectById(id);
    }

    @Override
    public List<StudyEvaluationDO> getEvaluationListByIds(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : evaluationMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudyEvaluationDO> getEvaluationPage(StudyEvaluationPageReqVO pageReqVO) {
        return evaluationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudyEvaluationDO> getEvaluationList(StudyEvaluationExportReqVO exportReqVO) {
        return evaluationMapper.selectList(exportReqVO);
    }



    /**
    * 获得学生评价列表, 根据 studentId 查询
    *
    * @param  studentId  学生id 查询条件
    * @return 学生评价列表
    */
    @Override
    public List<StudyEvaluationDO> getEvaluationListBystudentId(String studentId){
        return evaluationMapper.selectList(StudyEvaluationDO::getStudentId,studentId);
    }

    /**
    * 获得学生评价列表, 根据 teacherId 查询
    *
    * @param  teacherId  老师id 查询条件
    * @return 学生评价列表
    */
    @Override
    public List<StudyEvaluationDO> getEvaluationListByteacherId(String teacherId){
        return evaluationMapper.selectList(StudyEvaluationDO::getTeacherId,teacherId);
    }

    /**
    * 获得学生评价列表, 根据 orgId 查询
    *
    * @param  orgId  机构编号 查询条件
    * @return 学生评价列表
    */
    @Override
    public  List<StudyEvaluationDO> getEvaluationListByorgId(Integer orgId){
        return evaluationMapper.selectList(StudyEvaluationDO::getOrgId,orgId);
    }


}
